package main

import (
	"fmt"
	"time"
)

func main() {

	//2016-07-14 14:27:28.214512532 +0800 CST
	fmt.Println("本地当期时间",time.Now())
	fmt.Println("\n-----------------")
	// 2:27:05.702 PM Thu Jul
	fmt.Println("时间格式化",time.Now().Format("3:04:05.000 PM Mon Jan"))
	// 2016-07-14 2:54:11.442 PM Thu Jul
	fmt.Println("时间格式化",time.Now().Format("2006-01-_2 3:04:05.000 PM Mon Jan"))
	// 2016-07-14 14:54:11.442239513 +0800 CST
	fmt.Println("时间格式化",time.Now().Format("2006-01-02 15:04:05"))
	fmt.Println("时间格式化",time.Now().Format(" 2006-1-02 15:4:5"))

	fmt.Println("\n-----------------")
	fmt.Println("本地当前时间戳(10位)",time.Now().Unix()) //1468479251

	fmt.Println("本地当前时间戳(19位)",time.Now().UnixNano()) //1468480006774460462

	//2014-01-07 09:32:12
	fmt.Println("\n-----------------")
	fmt.Println("时间戳转时间",time.Unix(1389058332, 0).Format("2006-01-02 15:04:05"))

	fmt.Println("时间转时间戳",time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local).Unix())


	//时间转换为UTC时间和本地时间( UTC:零时区 +0000， China: 东八区 +0800)
	fmt.Println("\n-----------------")
	dateStr := "2016-07-14 14:24:51"
	timestamp1, _ := time.Parse("2006-01-02 15:04:05", dateStr)
	timestamp2, _ := time.ParseInLocation("2006-01-02 15:04:05", dateStr, time.Local)
	//2016-07-14 14:24:51 +0000 UTC 2016-07-14 14:24:51 +0800 CST
	fmt.Println("转换后变成时间格式",timestamp1, timestamp2)
	//1468506291 1468477491
	fmt.Println("转换后的时间戳",timestamp1.Unix(), timestamp2.Unix())

	now := time.Now()
	year, mon, day := now.UTC().Date()
	hour, min, sec := now.UTC().Clock()
	zone, _ := now.UTC().Zone()
	fmt.Printf("UTC 时间是 %d-%d-%d %02d:%02d:%02d %s\n",
		year, mon, day, hour, min, sec, zone) // UTC 时间是 2016-7-14 07:06:46 UTC

	year, mon, day = now.Date()
	hour, min, sec = now.Clock()
	zone, _ = now.Zone()
	fmt.Printf("本地时间是 %d-%d-%d %02d:%02d:%02d %s\n",
		year, mon, day, hour, min, sec, zone) // 本地时间是 2016-7-14 15:06:46 CST
}